﻿@using PagedList.Mvc;

@model PagedList.IPagedList<Video>

@section styles{
    @Styles.Render("~/Content/PagedList.css")
}

@functions {
    private string SortOrderLink(string displayName, string name, string search, int pageSize, bool isTrash, int productId)
    {
        string url = Url.Action("Index", new
        {
            sortOrder = ViewData[name + "Sort"],
            search = search,
            pageSize = pageSize,
            isTrash = isTrash,
            productId = productId
        });

        string icon = String.IsNullOrWhiteSpace(ViewData[name + "Icon"] as String)
            ? DataTableConstant.DefaultSortIcon
            : ViewData[name + "Icon"] as String;

        return "<a href='" + url + "'>" + displayName + icon + "</a>";
    }
}

@{
    string title = ViewBag.Title;
    string search = ViewBag.Search;
    string sortOrder = ViewBag.SortOrder;
    int pageSize = ViewBag.PageSize;
    bool isTrash = ViewBag.IsTrash;
    int trashCount = ViewBag.TrashCount;
    int listCount = ViewBag.ListCount;
    string returnListUrl = ViewBag.ReturnListUrl;
    SelectList pageSizeDDL = ViewBag.PageSizeDDL;
    Product product = ViewBag.Product;

    int index = Model.FirstItemOnPage;
    string controller = "video";

    ViewBag.Title = "Danh sách " + title;
}

<section class="content-header">
    <h1>
        @title.ToUpper() <small>@(!isTrash ? "danh sách" : "thùng rác")</small>
    </h1>
    <ol class="breadcrumb">
        <li>
            <a href="@Url.Action("Index", "Admin" )">
                <i class="fa fa-home"></i> Trang chủ
            </a>
        </li>
        <li>
            <a href="@Url.Action("Index", "Product")">
                Danh sách sản phẩm
            </a>
        </li>
        <li>
            <a href="@Url.Action("Index", "Product" , new { search = product.Code })">
                @product.Name - @product.Code
            </a>
        </li>
        <li class="active">@(!isTrash ? "Danh sách" : "Thùng rác") @title</li>
    </ol>
</section>

<section class="content">

    @Html.Action("Alert", "Layout", new { partialView = "_AlertHandling" })

    <div class="row">
        <div class="col-md-8">
            <div class="box box-primary">
                <div class="box-body">
                    @using (Html.BeginForm("Index", "Image", FormMethod.Get))
                    {
                        @Html.Hidden("IsTrash", isTrash)

                        @Html.DropDownList("PageSize", pageSizeDDL,
                        new
                        {
                            @class = "form-control auto-width inline",
                            onchange = "this.form.submit()"
                        })

                        if (!isTrash)
                        {
                            bool roleCreate = User.IsInRole("Admin") || User.IsInRole("Image Create");

                            if (roleCreate)
                            {
                                <a href="@Url.Action("Create", new { productId = product.Id, returnListUrl = returnListUrl })" class="btn btn-warning">
                                    <i class="fa fa-plus"></i> Tạo mới
                                </a>
                            }
                            <a href="@Url.Action("Index", new { productId = product.Id, isTrash = true })" class="btn btn-danger @(trashCount > 0 ? "" : "disabled")">
                                <i class="fa fa-trash-o"></i> Thùng rác (@trashCount)
                            </a>
                        }
                        else
                        {
                            <a href="@Url.Action("Index", new { productId = product.Id })" class="btn btn-success">
                                <i class="fa fa-file-text"></i> Danh sách (@listCount)
                            </a>
                        }
                    }

                    <div class="table-responsive">
                        <table class="table table-bordered table-hover">
                            <thead>
                                <tr>
                                    <th>#</th>
                                    <th></th>
                                    <th>
                                        @Html.Raw(SortOrderLink("Màu", "Property.Name", search, pageSize, isTrash, product.Id))
                                    </th>
                                    <th>
                                        @Html.Raw(SortOrderLink("Tạo mới", "DateCreated", search, pageSize, isTrash, product.Id))
                                    </th>
                                    <th>
                                        @Html.Raw(SortOrderLink("Chỉnh sửa", "DateModified", search, pageSize, isTrash, product.Id))
                                    </th>
                                    <th class="text-center">
                                        <a id="btnRefresh" href="@Request.Url.AbsoluteUri" class="btn btn-success btn-sm hidden">
                                            <i class="fa fa-refresh"></i> Làm mới
                                        </a>
                                    </th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach (var item in Model)
                                {
                                    bool roleEdit = User.IsInRole("Admin") || User.IsInRole("Video Edit Public")
                                       || (User.IsInRole("Video Edit Private") && item.CreatedBy == User.Identity.Name);

                                    bool roleRecycle = User.IsInRole("Admin") || User.IsInRole("Video Recycle Public")
                                        || (User.IsInRole("Video Recycle Private") && item.CreatedBy == User.Identity.Name);

                                    bool roleDelete = User.IsInRole("Admin") || User.IsInRole("Video Delete Public")
                                        || (User.IsInRole("Video Delete Private") && item.CreatedBy == User.Identity.Name);
                                    
                                    <tr>
                                        <td>
                                            @(index++)
                                        </td>
                                        <td class="no-padding">
                                            <video class="center-block no-href"
                                                   src="@item.Url"
                                                   width="50"
                                                   onmouseover="PreviewVideo(this)"
                                                   data-id="@item.Id"
                                                   data-toggle="modal"
                                                   data-target="#detailsModal"
                                                   title="Chi tiết"
                                                   onclick="Details(this, '@controller')" />
                                        </td>
                                        <td>
                                            @if (item.PropertyId != null)
                                            {
                                                if (item.Property.Description == null || item.Property.Name.Contains("/"))
                                                {
                                                    @item.Property.Name
                                                }
                                                else
                                                {
                                                    <i class="fa fa-square" style="color: @item.Property.Description"></i> @item.Property.Name
                                                }
                                            }
                                        </td>
                                        <td>
                                            <div>
                                                @item.DateCreated.ToString(DataTableConstant.DateTimeFormat)
                                            </div>
                                            <div>
                                                @item.CreatedBy
                                            </div>
                                        </td>
                                        <td>
                                            @if (item.DateModified != null)
                                            {
                                                <div>
                                                    @(((DateTime)item.DateModified).ToString(DataTableConstant.DateTimeFormat))
                                                </div>
                                                <div>
                                                    @item.ModifiedBy
                                                </div>
                                            }
                                        </td>
                                        <td class="control">
                                            @if (!isTrash)
                                            {
                                                if (roleEdit)
                                                {
                                                    <a href="@Url.Action("Edit", new { id = item.Id, returnListUrl = returnListUrl })"
                                                       data-toggle="tooltip" data-placement="top" title="Chỉnh sửa">
                                                        <i class="fa fa-edit"></i>
                                                    </a>
                                                }
                                                if (roleRecycle)
                                                {
                                                    <a class="no-href" data-id="@item.Id" data-toggle="tooltip" data-placement="top"
                                                       title="Xóa tạm thời" onclick="Recycle(this, '@controller', 0)">
                                                        <i class="fa fa-trash-o"></i>
                                                    </a>
                                                }
                                            }
                                            else
                                            {
                                                if (roleRecycle)
                                                {
                                                    <a class="no-href" data-id="@item.Id" data-toggle="tooltip" data-placement="top"
                                                       title="Khôi phục" onclick="Recycle(this, '@controller', 1)">
                                                        <i class="fa fa-repeat"></i>
                                                    </a>
                                                }
                                                if (roleDelete)
                                                {
                                                    <a class="no-href" data-id="@item.Id" data-toggle="modal" data-target="#deleteModal"
                                                       title="Xóa vĩnh viễn" onclick="Details(this, '@controller')">
                                                        <i class="fa fa-times"></i>
                                                    </a>
                                                }
                                            }
                                        </td>
                                    </tr>
                                }
                            </tbody>
                        </table>
                    </div>
                    <br />
                    <div class="row">
                        <div class="col-md-6">
                            <div class="well-sm">
                                Hiển thị <span class="badge">@Model.FirstItemOnPage</span>
                                đến <span class="badge">@Model.LastItemOnPage</span>
                                trong <span class="badge">@Model.TotalItemCount</span> @title
                            </div>
                        </div>
                        <div class="col-md-6 text-right">
                            @Html.PagedListPager(Model, page => Url.Action("Index", new
                            {
                                page,
                                sortOrder = sortOrder,
                                search = search,
                                pageSize = pageSize,
                                isTrash = isTrash,
                                productId = product.Id
                            }), new PagedListRenderOptions
                            {
                                LinkToFirstPageFormat = DataTableConstant.LinkToFirstPageFormat,
                                LinkToPreviousPageFormat = DataTableConstant.LinkToPreviousPageFormat,
                                LinkToNextPageFormat = DataTableConstant.LinkToNextPageFormat,
                                LinkToLastPageFormat = DataTableConstant.LinkToLastPageFormat,
                                MaximumPageNumbersToDisplay = 5
                            })
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            @if (Model.Count > 0)
            {
                Video video = Model.FirstOrDefault();

                <div class="box box-primary">
                    <div class="box-body">
                        <video id="vidPreview" class="img-responsive center-block" src="@video.Url" controls />
                    </div>
                </div>
            }
        </div>
    </div>
</section>

@Html.Action("ManagerModal", "Layout", new
{
    partialView = "_DetailsModal",
    title = title
})

@Html.Action("ManagerModal", "Layout", new
{
    partialView = "_DeleteModal",
    title = title,
    controller = controller,
    returnListUrl = returnListUrl
})

@section scripts{
    <script>
        ActiveMenu('product');

        function PreviewVideo(self) {
            var url = $(self).attr('src');

            var element = $('#vidPreview');
            element.hide().attr('src', url);
            element.fadeIn('slow');
        }
    </script>
}